package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPlan;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PayVO;
import com.digitalchina.lxnsy.entity.PaymentAmountVO;
import com.digitalchina.lxnsy.entity.PlantOrderVO;
import com.digitalchina.lxnsy.entity.PlantTempVO;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.wxapi.WXPayEntryActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2019/1/9 17:59
 * 修改备注：
 * ================================================
 */
public class PlantPayActivity extends BaseActivity {

    @Bind(R.id.tv_nb)
    TextView tvNb;
    @Bind(R.id.sw_nb)
    Switch swNb;
    @Bind(R.id.layout)
    RelativeLayout layout;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_deduction)
    TextView tvDeduction;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_price_total)
    TextView tvPriceTotal;
    @Bind(R.id.tv_next)
    TextView tvNext;

    private PlantTempVO vo;
    private String plantId = "";
    private String isCurrency = "1"; //是否开启农币付费 0开启，1不开启

    private String totalPriceReal = ""; // 实际支付金额
    private String goodPrice = ""; // 商品金额
    private Map<String, Object> map = new HashMap<>();
    private String type = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_plant_pay);
        setTitle("支付方式");
    }

    @Override
    public void initMember() {
        vo = (PlantTempVO) getIntent().getSerializableExtra("PlantTempVO");
        plantId = vo.getPlantId();
        type = vo.getType();
        BusinessPlan.getPaymentAmount(this, plantId, isCurrency, mHandler);

        ActivityTaskManager.getInstance().closeActivity(SelectProductionActivityV2.class);
    }

    @Override
    public void addListener() {
        swNb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b)
                    isCurrency = "0";
                else
                    isCurrency = "1";

                BusinessPlan.getPaymentAmount(PlantPayActivity.this, plantId, isCurrency, mHandler);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_PAYMENT_AMOUNT_SUCCESS: // 获取剩余农币及支付金额
                        /**
                         * actualAmountPaid 农币支付金额
                         * fee 支付总金额
                         * surplusAmount 扣除农币后需要支付金额
                         * pointTotalValue 用户总农币
                         * sysDataNbValue 农币折扣率
                         */
                        PaymentAmountVO paymentAmountvo = FastJsonUtil.getBean(msg.obj.toString(),"", PaymentAmountVO.class);
                        // 目前剩余XX农币，兑换汇率1:30（人民币：农币）
                        tvNb.setText("目前剩余" + paymentAmountvo.getPointTotalValue() + "农币，兑换汇率1:" + paymentAmountvo.getSysDataNbValue() + "（人民币：农币）");

                        tvPrice.setText("￥" + paymentAmountvo.getFee());
                        tvDeduction.setText("-￥" + (StringUtil.isStrEmpty(paymentAmountvo.getActualAmountPaid()) ? "0.00" : paymentAmountvo.getActualAmountPaid()));
                        tvPriceTotal.setText("￥" + paymentAmountvo.getSurplusAmount());
                        totalPriceReal = paymentAmountvo.getSurplusAmount();
                        goodPrice = paymentAmountvo.getFee();
                        break;
                    case MSG.MSG_GET_PAYMENT_AMOUNT_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_PLACE_ORDER_SUCCESS:
                        PlantOrderVO plantVO = FastJsonUtil.getBean(msg.obj.toString(), "plantOrder", PlantOrderVO.class);
                        if ("2".equals(plantVO.getPaymentStatus())){
                            // 农币抵扣免费的下单成功
                            ActivityTaskManager.getInstance().closeActivity(SelectProductionActivityV2.class);
                            map.put("type", type);
                            map.put("productName", vo.getName());
                            GotoUtil.gotoActivity(PlantPayActivity.this, TradeSuccessActivity.class, true, "map", map);
                        }else{
                            PayVO payVO = new PayVO();
                            payVO.setPrice(totalPriceReal);
                            payVO.setOrderNO(plantVO.getOrderNo());
                            payVO.setType(3);
                            payVO.setGoodsName(vo.getName());
                            GotoUtil.gotoActivity(PlantPayActivity.this, WXPayEntryActivity.class, true,"payVO", payVO);
                        }
                        break;
                    case MSG.MSG_PLACE_ORDER_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_next)
    public void onViewClicked() {
        if (!StringUtil.isStrEmpty(totalPriceReal))
            BusinessPlan.placeOrder(PlantPayActivity.this, plantId, goodPrice, isCurrency, mHandler);
    }
}
